<?php

namespace App\Http\Controllers;

use App\MasterVideo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class MasterVideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $mvideos = MasterVideo::latest('created_at')->Paginate(8);
        return view('MasterVideo.masterVideo',compact('mvideos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('MasterVideo.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules = [
            'video_name' => 'required',
            'video_intro' => 'required|min:2',
            'video_pic' => 'required',
            'savedpath' => 'required'
        ];
        $this->validate($request, $rules);
        $this->uploadAvatar($request);
        $masterVideoPic = $request->get('video_name')
            . '.' . $request->file('video_pic')
                ->getClientOriginalExtension();

        $masterVideo = new MasterVideo;
        $masterVideo->video_pic = $masterVideoPic;
        $masterVideo->video_address = $request->get('savedpath');
        $masterVideo->video_name = $request->get('video_name');
        $masterVideo->video_intro = $request->get('video_intro');
        $masterVideo->master_id  = 2;
        $masterVideo->save();

        session()->flash('success', '大师视频上传成功');

        return redirect()->route('mvideo.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $video = MasterVideo::find($id);
        $url = Storage::disk('videos')->url($video->video_address);
        return $url;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $masterVideo = MasterVideo::find($id);
        $avatar = $masterVideo->video_pic;
        $video = $masterVideo->video_address;
        if ($avatar != 'default.jpg') {
            Storage::disk('video_pic')->exists($avatar);
            Storage::disk('video_pic')->delete($avatar);
            Storage::disk('videos')->delete($video);
        }
        $masterVideo->delete();

        session()->flash('success', '删除成功');
        return redirect()->route('mvideo.index');
    }

    /**
     * Upload Avatar to public_path
     * @param Request $request
     */
    public function uploadAvatar(Request $request)
    {
        if ($request->isMethod('post') OR $request->isMethod('patch')) {

            $avatar = $request->file('video_pic');

            if ($avatar->isValid()) {

                $realPath = $avatar->getRealPath();
                $ext = $avatar->getClientOriginalExtension();
                $masterName = $request->get('video_name');
                $fileName = $masterName. '.' .$ext;
//              Storage::disk('uploads')->put($fileName, file_get_contents($realPath));
                //clip avatar
                Image::make($avatar)->fit(300, 300)->save(public_path('/uploads/video_pic/') . $fileName);

            }
        }
    }
}
